﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.IO;

using AimGameLibrary;
using AimGameLibrary.ScreenManager;

namespace AimGameEngine.Menu
{
    partial class Option : Form
    {
        SettingEditorData settingEditor;
        XMLData<SettingEditorData> xmlSetEditor;
        public Option()
        {
            InitializeComponent();
            //this.Text = String.Format("About {0} {0}", AssemblyTitle);

            xmlSetEditor = new XMLData<SettingEditorData>();
            if (File.Exists(Script.SettingFile))
                settingEditor = xmlSetEditor.Load(Script.SettingFile);
            else
            {
                settingEditor.SettingDefualt(Assembly.GetExecutingAssembly().Location);
                XMLData<SettingEditorData> load = new XMLData<SettingEditorData>();
                load.Save(settingEditor, Assembly.GetExecutingAssembly().Location);
            }

            this.pathBuildSource.Text = settingEditor.BuildOutput;
            this.pathMainScript.Text = settingEditor.MainScriptpath;
            this.EXEpath.Text = settingEditor.EXEpath;

            foreach (String str in ScreenResolution.GetListResolution())
                this.GameResolution.Items.Add(str);

            GameResolution.SelectedItem = ScreenResolution.GetNameResolution(settingEditor.Resolution);
        }

        #region Assembly Attribute Accessors

        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion


        private void Browse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            // Default to the directory which contains our content files.
            string assemblyLocation = Assembly.GetExecutingAssembly().Location;
            string relativePath = Path.Combine(assemblyLocation, "../../../../Content");
            string contentPath = Path.GetFullPath(relativePath);

            folder.SelectedPath = contentPath;
            // Show Down Title
            //folder.Description = "Test";

            if (folder.ShowDialog() == DialogResult.OK)
            {
                // LoadModel(fileDialog.FileName);
                Console.Write("OK Dialog" + "MyPath = " + folder.SelectedPath);

                pathBuildSource.Text = folder.SelectedPath;
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode.Name == "Output")
            {
                this.virtualForm1.SelectedIndex = 0;
            }
            else if (treeView1.SelectedNode.Name == "EXE path")
                this.virtualForm1.SelectedIndex = 1;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            settingEditor.BuildOutput = pathBuildSource.Text;
            settingEditor.MainScriptpath = pathMainScript.Text;
            settingEditor.EXEpath = EXEpath.Text;
            settingEditor.Resolution = ScreenResolution.GetResolution(GameResolution.SelectedItem.ToString());
            xmlSetEditor.Save(settingEditor, Script.SettingFile);
        }

        private void BrowseEXE_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            // Default to the directory which contains our content files.
            string assemblyLocation = Assembly.GetExecutingAssembly().Location;
            string relativePath = Path.Combine(assemblyLocation, "../../../../Content");
            string contentPath = Path.GetFullPath(relativePath);

            file.InitialDirectory = contentPath;

            file.Title = "Game Management EXE path";

            file.Filter = "EXE Files (*.exe)|*.exe|" +
                                "All Files (*.*)|*.*";

            if (file.ShowDialog() == DialogResult.OK)
            {
                this.EXEpath.Text = file.FileName;
            }

        }

        private void BrowseMainScript_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            // Default to the directory which contains our content files.
            string assemblyLocation = Assembly.GetExecutingAssembly().Location;
            string relativePath = Path.Combine(assemblyLocation, "../../../../Content");
            string contentPath = Path.GetFullPath(relativePath);

            folder.SelectedPath = contentPath;
            // Show Down Title
            //folder.Description = "Test";

            if (folder.ShowDialog() == DialogResult.OK)
            {
                // LoadModel(fileDialog.FileName);
                Console.Write("OK Dialog" + "MyPath = " + folder.SelectedPath);

                pathMainScript.Text = folder.SelectedPath;
            }
        }


    }
}
